﻿using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Utils;
using Entity;
using DataBaseConnect.App;
using System.Security.Claims;
using DataBaseConnect.IRepositories;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace CoreMVC.Jurisdiction.Manage.Controllers
{
    public class IndexController : BaseController
    {

        #region  构造实例化

        public IMenuAppService _menuAppService;
        public IMenuRepository _menuRepository;
        public IndexController(IMenuAppService menuAppService, IMenuRepository menuRepository)
        {
            _menuAppService = menuAppService;
            _menuRepository = menuRepository;
        }

        #endregion

        #region 视图区

        /// <summary>
        /// 添加菜单
        /// </summary>
        /// <returns></returns>
        public IActionResult AddMneu()
        {
            return View();
        }

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <returns></returns>
        public IActionResult EditMenu(int menuId)
        {
            var menu = _menuAppService.GetMenuFirst(menuId);
            return View(menu);
        }

        #endregion

        #region 功能实现区

        /// <summary>
        /// 获取菜单列表（分页）
        /// </summary>
        /// <param name="MenuName">菜单名</param>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页显示条数</param>
        /// <returns></returns>
        public JsonResult GetAllMenuList(string MenuName, int page, int limit)
        {
            ReceiveStatus<Menu> receiveStatus = new ReceiveStatus<Menu>();
            List<Menu> list = new List<Menu>();
            int rowCount = 0;
            try
            {
                //获取cookie里的userid
                string userId = User.FindFirst(ClaimTypes.Sid).Value;
                int userIds = int.Parse(userId);
                //得到所有菜单
                list = _menuAppService.GetMenuListPage(MenuName, page, limit, out rowCount);
                receiveStatus.msg = "成功";
                receiveStatus.data = list;
                receiveStatus.count = rowCount;
            }
            catch (System.Exception ex)
            {
                receiveStatus.msg = "失败：" + ex.Message;
                receiveStatus.code = statuCode.Error;
            }
            return Json(receiveStatus);
        }

        /// <summary>
        ///  添加菜单信息
        /// </summary>
        /// <param name="input">菜单信息实体</param>
        /// <returns></returns>
        public JsonResult AddMenuMsg(Menu input)
        {
            return Json(_menuAppService.AddMenuMsg(input));
        }

        /// <summary>
        /// 编辑菜单信息
        /// </summary>
        /// <param name="input">菜单信息实体</param>
        /// <returns></returns>
        public JsonResult EditMenuMsg(Menu input)
        {
            return Json(_menuAppService.EditMenuMsg(input));
        }

        /// <summary>
        /// 递归删除菜单
        /// 删除菜单权限
        /// 删除菜单按钮权限
        /// 删除菜单按钮
        /// </summary>
        /// <param name="menuId">菜单id集合</param>
        /// <returns></returns>
        public JsonResult DelMenu(List<Menu> menuId)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            foreach (var item in menuId)
            {
                receiveStatus = _menuAppService.DelMenu(item.MenuId);
            }
            return Json(receiveStatus) ;
        }

        #endregion
    }
}
